package io.esirong.widget.view;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.drawable.GradientDrawable;
import android.graphics.drawable.StateListDrawable;
import android.util.AttributeSet;
import android.widget.Button;

/**
 */
public class ProgressButton extends Button {

    /*
     *思路：背景层的圆角要一致。
     * 按击的效果，要使用半透明层
     */
    private float mCornerRadius = 0;
    private float mProgressMargin = 0;

    private int mProgress; //当前进度
    private int mMaxProgress = 100; //最大进度：默认为100
    private int mMinProgress = 0;//最小进度：默认为0
    private GradientDrawable mProgressDrawable;// 加载进度时的进度颜色
    private GradientDrawable mProgressDrawableBg;// 加载进度时的背景色
    private StateListDrawable mNormalDrawable; // 按钮在不同状态的颜色效果

    private boolean isShowProgress;  //是否展示进度
    private boolean isFinish; // 结束状态
    private boolean isStop;// 停止状态
    private boolean isStart; // 刚开始的状态
    private OnStateListener onStateListener; //结束时的监听
    private float cornerRadius; // 圆角半径

    public ProgressButton(Context context) {
        super(context);
    }

    public ProgressButton(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public ProgressButton(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    private void init(Context context, AttributeSet attrs) {
//
//        LayoutInflater.from(context).inflate(R.layout.widget_xx, this, true);
//
//        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.xx);
//
//        try {
//
//        } finally {
//            typedArray.recycle();
//        }

        initView();
    }

    private void initView() {
        // 设置圆角
        mProgressDrawable.setCornerRadius(cornerRadius);
        mProgressDrawableBg.setCornerRadius(cornerRadius);
        setBackground(mNormalDrawable);
    }

    @Override
    protected void onDraw(Canvas canvas) {

        //更新进度（绘制）
        if (getProgress() > getMinProgress() && getProgress() <= getMaxProgress()) {
            float scale = (float) getProgress() / (float) getMaxProgress();
            float indicatorWidth = (float) getMeasuredWidth() * scale;

            mProgressDrawable.setBounds(0, 0, (int) indicatorWidth, getMeasuredHeight());
            mProgressDrawable.draw(canvas);
        }

        // 进度完成时回调方法，并更变状态
        if (mProgress == mMaxProgress) {
            setBackground(mProgressDrawable);
            isFinish = true;
            if (onStateListener != null) {
                onStateListener.onFinish();
            }
        }

        super.onDraw(canvas);
    }

    public int getMinProgress() {
        return mMinProgress;
    }

    public int getMaxProgress() {
        return mMaxProgress;
    }

    public int getProgress() {
        return mProgress;
    }

    /**
     * 设置进度
     *
     * @param progress 进度值
     */
    public void setProgress(int progress) {
        mProgress = progress;
        if (isShowProgress) {
            setText(String.format("%d %%", mProgress));
        }
        postInvalidate();
    }


    /**
     * 回调
     */
    interface OnStateListener {

        void onFinish();

        void onStop();

        void onContinue();

    }
}
